﻿using MyFileView.Shard;

using System.IO;
using System;
using MyFileView.Shard.Model;
using System.Web.Hosting;
using MyFileView.Core.Constant;
using MyFileView.Core;
using System.Web;
using System.Collections.Generic;
using ImageMagick;

namespace MyFleView.Core
{
    public class ConvertFileInfo
    {
        public string Id { get; set; }=Guid.NewGuid().ToString("N");

      

        public string FileName { get; set; }

        public string ContentType { get; set; }

        public string FilePath { get; set; } = string.Empty;

        public string FileType { get; set; } = string.Empty;

        public string FileSuffix { get; set; } = string.Empty;
        public string FileSize{ get; set; } 


      
        /// <summary>
        /// 完整文件路径,libreoffice用到
        /// </summary>
        public string FullFilePath { get { return FilePath + Slash + FileName; } }


        public string OfficeFullFilePath { get { return PathConverter(FilePath + Slash + FileName); } }


        public string OutPutFileName { get { return FileName+ Point + OutPutFileSuffix; } }


        private string Point { get; } = PathConstant.Point;

        //斜杠
        private string Slash { get; } = PathConstant.Slash;

        public string OutPutFilePath
        {
            get { return outPath; }
        }

        private string outPath;


        public string OfficeOutPutFullFilePath
        {
            get { return PathConverter(outPath); }
            set { outPath = FilePath + Slash + FileName + Point + value; }
        }


        public string OutPutFileSuffix
        {
            get { return OfficeOutPutFullFilePath.Substring(OfficeOutPutFullFilePath.LastIndexOf(Point) + 1); }

        }


        public string UrlSuffix { get; set; }


     //   public List<string> ImagePreviewPath { get; set; } = new List<string>();
    
        //public List<string> GenerateUrl() //string host
        //{
        //    List<string> urls = new List<string>();
        //    foreach (var item in ImagePreviewPath)
        //    {
        //        if (item.Contains(UrlSuffix)) 
        //        {
        //            int index = item.LastIndexOf(UrlSuffix);
        //            string url= item.Substring(index, item.Length- index);
        //            urls.Add(url);
        //        }    
        //    }
        //    return urls;
        //}

       // public void SetMagickImage(MagickImageCollection magickImages)
       // {
       ////     ImagePreviewPath = new();
       //     if (magickImages.Count > 0)
       //     {
       //         foreach (var image in magickImages)
       //         {
       //             ImagePreviewPath.Add(image.FileName);
       //         }
       //     }

       // }

        public static  ConvertFileInfo Create(PreviewFileRequestModel uploadFileModel,HttpRequestBase httpRequestBase)
        {
            return new ConvertFileInfo()
            {

                //   Id = uploadFileModel.Id,
                FileName = uploadFileModel.FileName,
                // ContentType = uploadFileModel.ContentType,
                //  FileType = uploadFileModel.FileType,
                FileSuffix = uploadFileModel.FileSuffix,
                FilePath = HostingEnvironment.ApplicationPhysicalPath + PathConstant.File + PathConstant.Slash+ uploadFileModel.FileName,
                UrlSuffix = PathConstant.File + PathConstant.Slash + uploadFileModel.FileName
            };
        }


        private string PathConverter(string file)
        {
            try
            {
                file = file.Replace(@"\", "/");

                return "file:///" + file;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="stream"></param>
        public void ComputeFileSize(Stream stream) 
        {
            string[] sizes = { "B", "KB", "MB", "GB", "TB" };
            double len = stream.Length;
            int order = 0;
            while (len >= 1024 && order < sizes.Length - 1)
            {
                order++;
                len = len / 1024;
            }
            string result = String.Format("{0:0.##} {1}", len, sizes[order]);
        }

    }
}